home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / make.info-3 (.txt) < prev    next >
GNU Info File  |  1993-05-16  |  51KB  |  910 lines

  1. This is Info file make.info, produced by Makeinfo-1.54 from the input
  2. file make.texinfo.
  3.    This file documents the GNU Make utility, which determines
  4. automatically which pieces of a large program need to be recompiled,
  5. and issues the commands to recompile them.
  6.    This is Edition 0.42, last updated 14 May 1993, of `The GNU Make
  7. Manual', for `make', Version 3.66 Beta.
  8.    Copyright (C) 1988, '89, '90, '91, '92, '93 Free Software
  9. Foundation, Inc.
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided also
  15. that the section entitled "GNU General Public License" is included
  16. exactly as in the original, and provided that the entire resulting
  17. derived work is distributed under the terms of a permission notice
  18. identical to this one.
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the text of the translations of the section
  22. entitled "GNU General Public License" must be approved for accuracy by
  23. the Foundation.
  24. File: make.info,  Node: Static Usage,  Next: Static versus Implicit,  Up: Static Pattern
  25. Syntax of Static Pattern Rules
  26. ------------------------------
  27.    Here is the syntax of a static pattern rule:
  28.      TARGETS ...: TARGET-PATTERN: DEP-PATTERNS ...
  29.              COMMANDS
  30.              ...
  31. The TARGETS list specifies the targets that the rule applies to.  The
  32. targets can contain wildcard characters, just like the targets of
  33. ordinary rules (*note Using Wildcard Characters in File Names:
  34. Wildcards.).
  35.    The TARGET-PATTERN and DEP-PATTERNS say how to compute the
  36. dependencies of each target.  Each target is matched against the
  37. TARGET-PATTERN to extract a part of the target name, called the "stem".
  38. This stem is substituted into each of the DEP-PATTERNS to make the
  39. dependency names (one from each DEP-PATTERN).
  40.    Each pattern normally contains the character `%' just once.  When the
  41. TARGET-PATTERN matches a target, the `%' can match any part of the
  42. target name; this part is called the "stem".  The rest of the pattern
  43. must match exactly.  For example, the target `foo.o' matches the
  44. pattern `%.o', with `foo' as the stem.  The targets `foo.c' and
  45. `foo.out' do not match that pattern.
  46.    The dependency names for each target are made by substituting the
  47. stem for the `%' in each dependency pattern.  For example, if one
  48. dependency pattern is `%.c', then substitution of the stem `foo' gives
  49. the dependency name `foo.c'.  It is legitimate to write a dependency
  50. pattern that does not contain `%'; then this dependency is the same for
  51. all targets.
  52.    `%' characters in pattern rules can be quoted with preceding
  53. backslashes (`\').  Backslashes that would otherwise quote `%'
  54. characters can be quoted with more backslashes.  Backslashes that quote
  55. `%' characters or other backslashes are removed from the pattern before
  56. it is compared to file names or has a stem substituted into it.
  57. Backslashes that are not in danger of quoting `%' characters go
  58. unmolested.  For example, the pattern `the\%weird\\%pattern\\' has
  59. `the%weird\' preceding the operative `%' character, and `pattern\\'
  60. following it.  The final two backslashes are left alone because they
  61. cannot affect any `%' character.
  62.    Here is an example, which compiles each of `foo.o' and `bar.o' from
  63. the corresponding `.c' file:
  64.      objects = foo.o bar.o
  65.      
  66.      $(objects): %.o: %.c
  67.              $(CC) -c $(CFLAGS) $< -o $@
  68. Here `$<' is the automatic variable that holds the name of the
  69. dependency and `$@' is the automatic variable that holds the name of
  70. the target; see *Note Automatic Variables: Automatic.
  71.    Each target specified must match the target pattern; a warning is
  72. issued for each target that does not.  If you have a list of files,
  73. only some of which will match the pattern, you can use the `filter'
  74. function to remove nonmatching file names (*note Functions for String
  75. Substitution and Analysis: Text Functions.):
  76.      files = foo.elc bar.o lose.o
  77.      
  78.      $(filter %.o,$(files)): %.o: %.c
  79.              $(CC) -c $(CFLAGS) $< -o $@
  80.      $(filter %.elc,$(files)): %.elc: %.el
  81.              emacs -f batch-byte-compile $<
  82. Here the result of `$(filter %.o,$(files))' is `bar.o lose.o', and the
  83. first static pattern rule causes each of these object files to be
  84. updated by compiling the corresponding C source file.  The result of
  85. `$(filter %.elc,$(files))' is `foo.elc', so that file is made from
  86. `foo.el'.
  87.    Another example shows how to use `$*' in static pattern rules:
  88.      bigoutput littleoutput : %output : text.g
  89.              generate text.g -$* > $@
  90. When the `generate' command is run, `$*' will expand to the stem,
  91. either `big' or `little'.
  92. File: make.info,  Node: Static versus Implicit,  Prev: Static Usage,  Up: Static Pattern
  93. Static Pattern Rules versus Implicit Rules
  94. ------------------------------------------
  95.    A static pattern rule has much in common with an implicit rule
  96. defined as a pattern rule (*note Defining and Redefining Pattern Rules:
  97. Pattern Rules.).  Both have a pattern for the target and patterns for
  98. constructing the names of dependencies.  The difference is in how
  99. `make' decides *when* the rule applies.
  100.    An implicit rule *can* apply to any target that matches its pattern,
  101. but it *does* apply only when the target has no commands otherwise
  102. specified, and only when the dependencies can be found.  If more than
  103. one implicit rule appears applicable, only one applies; the choice
  104. depends on the order of rules.
  105.    By contrast, a static pattern rule applies to the precise list of
  106. targets that you specify in the rule.  It cannot apply to any other
  107. target and it invariably does apply to each of the targets specified.
  108. If two conflicting rules apply, and both have commands, that's an error.
  109.    The static pattern rule can be better than an implicit rule for these
  110. reasons:
  111.    * You may wish to override the usual implicit rule for a few files
  112.      whose names cannot be categorized syntactically but can be given
  113.      in an explicit list.
  114.    * If you cannot be sure of the precise contents of the directories
  115.      you are using, you may not be sure which other irrelevant files
  116.      might lead `make' to use the wrong implicit rule.  The choice
  117.      might depend on the order in which the implicit rule search is
  118.      done.  With static pattern rules, there is no uncertainty: each
  119.      rule applies to precisely the targets specified.
  120. File: make.info,  Node: Double-Colon,  Next: Automatic Dependencies,  Prev: Static Pattern,  Up: Rules
  121. Double-Colon Rules
  122. ==================
  123.    "Double-colon" rules are rules written with `::' instead of `:'
  124. after the target names.  They are handled differently from ordinary
  125. rules when the same target appears in more than one rule.
  126.    When a target appears in multiple rules, all the rules must be the
  127. same type: all ordinary, or all double-colon.  If they are
  128. double-colon, each of them is independent of the others.  Each
  129. double-colon rule's commands are executed if the target is older than
  130. any dependencies of that rule.  This can result in executing none, any,
  131. or all of the double-colon rules.
  132.    Double-colon rules with the same target are in fact completely
  133. separate from one another.  Each double-colon rule is processed
  134. individually, just as rules with different targets are processed.
  135.    The double-colon rules for a target are executed in the order they
  136. appear in the makefile.  However, the cases where double-colon rules
  137. really make sense are those where the order of executing the commands
  138. would not matter.
  139.    Double-colon rules are somewhat obscure and not often very useful;
  140. they provide a mechanism for cases in which the method used to update a
  141. target differs depending on which dependency files caused the update,
  142. and such cases are rare.
  143.    Each double-colon rule should specify commands; if it does not, an
  144. implicit rule will be used if one applies.  *Note Using Implicit Rules:
  145. Implicit Rules.
  146. File: make.info,  Node: Automatic Dependencies,  Prev: Double-Colon,  Up: Rules
  147. Generating Dependencies Automatically
  148. =====================================
  149.    In the makefile for a program, many of the rules you need to write
  150. often say only that some object file depends on some header file.  For
  151. example, if `main.c' uses `defs.h' via an `#include', you would write:
  152.      main.o: defs.h
  153. You need this rule so that `make' knows that it must remake `main.o'
  154. whenever `defs.h' changes.  You can see that for a large program you
  155. would have to write dozens of such rules in your makefile.  And, you
  156. must always be very careful to update the makefile every time you add
  157. or remove an `#include'.
  158.    To avoid this hassle, most modern C compilers can write these rules
  159. for you, by looking at the `#include' lines in the source files.
  160. Usually this is done with the `-M' option to the compiler.  For
  161. example, the command:
  162.      cc -M main.c
  163. generates the output:
  164.      main.o : main.c defs.h
  165. Thus you no longer have to write all those rules yourself.  The
  166. compiler will do it for you.
  167.    With old `make' programs, it was traditional practice to use this
  168. compiler feature to generate dependencies on demand with a command like
  169. `make depend'.  That command would create a file `depend' containing
  170. all the automatically-generated dependencies; then the makefile could
  171. use `include' to read them in (*note Include::.).
  172.    In GNU `make', the feature of remaking makefiles makes this practice
  173. obsolete--you need never tell `make' explicitly to regenerate the
  174. dependencies, because it always regenerates any makefile that is out of
  175. date.  *Note Remaking Makefiles::.
  176.    The practice we recommend for automatic dependency generation is to
  177. have one makefile corresponding to each source file.  For each source
  178. file `NAME.c' there is a makefile `NAME.d' which lists what files the
  179. object file `NAME.o' depends on.  That way only the source files that
  180. have changed need to be rescanned to produce the new dependencies.
  181.    Here is the pattern rule to generate a file of dependencies (i.e., a
  182. makefile) called `NAME.d' from a C source file called `NAME.c':
  183.      %.d: %.c
  184.          $(CC) -M $(CPPFLAGS) $< | sed 's/$*.o/& $@/g' > $@
  185. *Note Pattern Rules::, for information on defining pattern rules.  The
  186. purpose of the `sed' command is to translate (for example):
  187.      main.o : main.c defs.h
  188. into:
  189.      main.o main.d : main.c defs.h
  190. This makes each `.d' file depend on all the source and header files
  191. that the corresponding `.o' file depends on.  `make' then knows it must
  192. regenerate the dependencies whenever any of the source or header files
  193. changes.
  194.    Once you've defined the rule to remake the `.d' files, you then use
  195. the `include' directive to read them all in.  *Note Include::.  For
  196. example:
  197.      sources = foo.c bar.c
  198.      
  199.      include $(sources:.c=.d)
  200. (This example uses a substitution variable reference to translate the
  201. list of source files `foo.c bar.c' into a list of dependency makefiles,
  202. `foo.d bar.d'.  *Note Substitution Refs::, for full information on
  203. substitution references.)  Since the `.d' files are makefiles like any
  204. others, `make' will remake them as necessary with no further work from
  205. you.  *Note Remaking Makefiles::.
  206. File: make.info,  Node: Commands,  Next: Using Variables,  Prev: Rules,  Up: Top
  207. Writing the Commands in Rules
  208. *****************************
  209.    The commands of a rule consist of shell command lines to be executed
  210. one by one.  Each command line must start with a tab, except that the
  211. first command line may be attached to the target-and-dependencies line
  212. with a semicolon in between.  Blank lines and lines of just comments
  213. may appear among the command lines; they are ignored.
  214.    Users use many different shell programs, but commands in makefiles
  215. are always interpreted by `/bin/sh' unless the makefile specifies
  216. otherwise.  *Note Command Execution: Execution.
  217.    The shell that is in use determines whether comments can be written
  218. on command lines, and what syntax they use.  When the shell is
  219. `/bin/sh', a `#' starts a comment that extends to the end of the line.
  220. The `#' does not have to be at the beginning of a line.  Text on a line
  221. before a `#' is not part of the comment.
  222. * Menu:
  223. * Echoing::                     How to control when commands are echoed.
  224. * Execution::                   How commands are executed.
  225. * Parallel::                    How commands can be executed in parallel.
  226. * Errors::                      What happens after a command execution error.
  227. * Interrupts::                  What happens when a command is interrupted.
  228. * Recursion::                   Invoking `make' from makefiles.
  229. * Sequences::                   Defining canned sequences of commands.
  230. * Empty Commands::              Defining useful, do-nothing commands.
  231. File: make.info,  Node: Echoing,  Next: Execution,  Up: Commands
  232. Command Echoing
  233. ===============
  234.    Normally `make' prints each command line before it is executed.  We
  235. call this "echoing" because it gives the appearance that you are typing
  236. the commands yourself.
  237.    When a line starts with `@', the echoing of that line is suppressed.
  238. The `@' is discarded before the command is passed to the shell.
  239. Typically you would use this for a command whose only effect is to print
  240. something, such as an `echo' command to indicate progress through the
  241. makefile:
  242.      @echo About to make distribution files
  243.    When `make' is given the flag `-n' or `--just-print', echoing is all
  244. that happens, no execution.  *Note Summary of Options: Options Summary.
  245. In this case and only this case, even the commands starting with `@'
  246. are printed.  This flag is useful for finding out which commands `make'
  247. thinks are necessary without actually doing them.
  248.    The `-s' or `--silent' flag to `make' prevents all echoing, as if
  249. all commands started with `@'.  A rule in the makefile for the special
  250. target `.SILENT' has the same effect (*note Special Built-in Target
  251. Names: Special Targets.).  `.SILENT' is essentially obsolete since `@'
  252. is more flexible.
  253. File: make.info,  Node: Execution,  Next: Parallel,  Prev: Echoing,  Up: Commands
  254. Command Execution
  255. =================
  256.    When it is time to execute commands to update a target, they are
  257. executed by making a new subshell for each line.  (In practice, `make'
  258. may take shortcuts that do not affect the results.)
  259.    *Please note:* this implies that shell commands such as `cd' that
  260. set variables local to each process will not affect the following
  261. command lines.  If you want to use `cd' to affect the next command, put
  262. the two on a single line with a semicolon between them.  Then `make'
  263. will consider them a single command and pass them, together, to a shell
  264. which will execute them in sequence.  For example:
  265.      foo : bar/lose
  266.              cd bar; gobble lose > ../foo
  267.    If you would like to split a single shell command into multiple
  268. lines of text, you must use a backslash at the end of all but the last
  269. subline.  Such a sequence of lines is combined into a single line, by
  270. deleting the backslash-newline sequences, before passing it to the
  271. shell.  Thus, the following is equivalent to the preceding example:
  272.      foo : bar/lose
  273.              cd bar;  \
  274.              gobble lose > ../foo
  275.    The program used as the shell is taken from the variable `SHELL'.
  276. By default, the program `/bin/sh' is used.
  277.    Unlike most variables, the variable `SHELL' is never set from the
  278. environment.  This is because the `SHELL' environment variable is used
  279. to specify your personal choice of shell program for interactive use.
  280. It would be very bad for personal choices like this to affect the
  281. functioning of makefiles.  *Note Variables from the Environment:
  282. Environment.
  283. File: make.info,  Node: Parallel,  Next: Errors,  Prev: Execution,  Up: Commands
  284. Parallel Execution
  285. ==================
  286.    GNU `make' knows how to execute several commands at once.  Normally,
  287. `make' will execute only one command at a time, waiting for it to
  288. finish before executing the next.  However, the `-j' or `--jobs' option
  289. tells `make' to execute many commands simultaneously.
  290.    If the `-j' option is followed by an integer, this is the number of
  291. commands to execute at once; this is called the number of "job slots".
  292. If there is nothing looking like an integer after the `-j' option,
  293. there is no limit on the number of job slots.  The default number of job
  294. slots is one, which means serial execution (one thing at a time).
  295.    One unpleasant consequence of running several commands
  296. simultaneously is that output from all of the commands comes when the
  297. commands send it, so messages from different commands may be
  298. interspersed.
  299.    Another problem is that two processes cannot both take input from the
  300. same device; so to make sure that only one command tries to take input
  301. from the terminal at once, `make' will invalidate the standard input
  302. streams of all but one running command.  This means that attempting to
  303. read from standard input will usually be a fatal error (a `Broken pipe'
  304. signal) for most child processes if there are several.
  305.    It is unpredictable which command will have a valid standard input
  306. stream (which will come from the terminal, or wherever you redirect the
  307. standard input of `make').  The first command run will always get it
  308. first, and the first command started after that one finishes will get
  309. it next, and so on.
  310.    We will change how this aspect of `make' works if we find a better
  311. alternative.  In the mean time, you should not rely on any command using
  312. standard input at all if you are using the parallel execution feature;
  313. but if you are not using this feature, then standard input works
  314. normally in all commands.
  315.    If a command fails (is killed by a signal or exits with a nonzero
  316. status), and errors are not ignored for that command (*note Errors in
  317. Commands: Errors.), the remaining command lines to remake the same
  318. target will not be run.  If a command fails and the `-k' or
  319. `--keep-going' option was not given (*note Summary of Options: Options
  320. Summary.), `make' aborts execution.  If make terminates for any reason
  321. (including a signal) with child processes running, it waits for them to
  322. finish before actually exiting.
  323.    When the system is heavily loaded, you will probably want to run
  324. fewer jobs than when it is lightly loaded.  You can use the `-l' option
  325. to tell `make' to limit the number of jobs to run at once, based on the
  326. load average.  The `-l' or `--max-load' option is followed by a
  327. floating-point number.  For example,
  328.      -l 2.5
  329. will not let `make' start more than one job if the load average is
  330. above 2.5.  The `-l' option with no following number removes the load
  331. limit, if one was given with a previous `-l' option.
  332.    More precisely, when `make' goes to start up a job, and it already
  333. has at least one job running, it checks the current load average; if it
  334. is not lower than the limit given with `-l', `make' waits until the load
  335. average goes below that limit, or until all the other jobs finish.
  336.    By default, there is no load limit.
  337. File: make.info,  Node: Errors,  Next: Interrupts,  Prev: Parallel,  Up: Commands
  338. Errors in Commands
  339. ==================
  340.    After each shell command returns, `make' looks at its exit status.
  341. If the command completed successfully, the next command line is executed
  342. in a new shell; after the last command line is finished, the rule is
  343. finished.
  344.    If there is an error (the exit status is nonzero), `make' gives up on
  345. the current rule, and perhaps on all rules.
  346.    Sometimes the failure of a certain command does not indicate a
  347. problem.  For example, you may use the `mkdir' command to ensure that a
  348. directory exists.  If the directory already exists, `mkdir' will report
  349. an error, but you probably want `make' to continue regardless.
  350.    To ignore errors in a command line, write a `-' at the beginning of
  351. the line's text (after the initial tab).  The `-' is discarded before
  352. the command is passed to the shell for execution.
  353.    For example,
  354.      clean:
  355.              -rm -f *.o
  356. This causes `rm' to continue even if it is unable to remove a file.
  357.    When you run `make' with the `-i' or `--ignore-errors' flag, errors
  358. are ignored in all commands of all rules.  A rule in the makefile for
  359. the special target `.IGNORE' has the same effect.  These ways of
  360. ignoring errors are obsolete because `-' is more flexible.
  361.    When errors are to be ignored, because of either a `-' or the `-i'
  362. flag, `make' treats an error return just like success, except that it
  363. prints out a message that tells you the status code the command exited
  364. with, and says that the error has been ignored.
  365.    When an error happens that `make' has not been told to ignore, it
  366. implies that the current target cannot be correctly remade, and neither
  367. can any other that depends on it either directly or indirectly.  No
  368. further commands will be executed for these targets, since their
  369. preconditions have not been achieved.
  370.    Normally `make' gives up immediately in this circumstance, returning
  371. a nonzero status.  However, if the `-k' or `--keep-going' flag is
  372. specified, `make' continues to consider the other dependencies of the
  373. pending targets, remaking them if necessary, before it gives up and
  374. returns nonzero status.  For example, after an error in compiling one
  375. object file, `make -k' will continue compiling other object files even
  376. though it already knows that linking them will be impossible.  *Note
  377. Summary of Options: Options Summary.
  378.    The usual behavior assumes that your purpose is to get the specified
  379. targets up to date; once `make' learns that this is impossible, it
  380. might as well report the failure immediately.  The `-k' option says
  381. that the real purpose is to test as many of the changes made in the
  382. program as possible, perhaps to find several independent problems so
  383. that you can correct them all before the next attempt to compile.  This
  384. is why Emacs' `compile' command passes the `-k' flag by default.
  385. File: make.info,  Node: Interrupts,  Next: Recursion,  Prev: Errors,  Up: Commands
  386. Interrupting or Killing `make'
  387. ==============================
  388.    If `make' gets a fatal signal while a command is executing, it may
  389. delete the target file that the command was supposed to update.  This is
  390. done if the target file's last-modification time has changed since
  391. `make' first checked it.
  392.    The purpose of deleting the target is to make sure that it is remade
  393. from scratch when `make' is next run.  Why is this?  Suppose you type
  394. `Ctrl-c' while a compiler is running, and it has begun to write an
  395. object file `foo.o'.  The `Ctrl-c' kills the compiler, resulting in an
  396. incomplete file whose last-modification time is newer than the source
  397. file `foo.c'.  But `make' also receives the `Ctrl-c' signal and deletes
  398. this incomplete file.  If `make' did not do this, the next invocation
  399. of `make' would think that `foo.o' did not require updating--resulting
  400. in a strange error message from the linker when it tries to link an
  401. object file half of which is missing.
  402.    You can prevent the deletion of a target file in this way by making
  403. the special target `.PRECIOUS' depend on it.  Before remaking a target,
  404. `make' checks to see whether it appears on the dependencies of
  405. `.PRECIOUS', and thereby decides whether the target should be deleted
  406. if a signal happens.  Some reasons why you might do this are that the
  407. target is updated in some atomic fashion, or exists only to record a
  408. modification-time (its contents do not matter), or must exist at all
  409. times to prevent other sorts of trouble.
  410. File: make.info,  Node: Recursion,  Next: Sequences,  Prev: Interrupts,  Up: Commands
  411. Recursive Use of `make'
  412. =======================
  413.    Recursive use of `make' means using `make' as a command in a
  414. makefile.  This technique is useful when you want separate makefiles for
  415. various subsystems that compose a larger system.  For example, suppose
  416. you have a subdirectory `subdir' which has its own makefile, and you
  417. would like the containing directory's makefile to run `make' on the
  418. subdirectory.  You can do it by writing this:
  419.      subsystem:
  420.              cd subdir; $(MAKE)
  421. or, equivalently, this (*note Summary of Options: Options Summary.):
  422.      subsystem:
  423.              $(MAKE) -C subdir
  424.    You can write recursive `make' commands just by copying this example,
  425. but there are many things to know about how they work and why, and about
  426. how the sub-`make' relates to the top-level `make'.
  427. * Menu:
  428. * MAKE Variable::               The special effects of using `$(MAKE)'.
  429. * Variables/Recursion::         How to communicate variables to a sub-`make'.
  430. * Options/Recursion::           How to communicate options to a sub-`make'.
  431. * -w Option::                   How the `-w' or `--print-directory' option
  432.                                  helps debug use of recursive `make' commands.
  433. File: make.info,  Node: MAKE Variable,  Next: Variables/Recursion,  Up: Recursion
  434. How the `MAKE' Variable Works
  435. -----------------------------
  436.    Recursive `make' commands should always use the variable `MAKE', not
  437. the explicit command name `make', as shown here:
  438.      subsystem:
  439.              cd subdir; $(MAKE)
  440.    The value of this variable is the file name with which `make' was
  441. invoked.  If this file name was `/bin/make', then the command executed
  442. is `cd subdir; /bin/make'.  If you use a special version of `make' to
  443. run the top-level makefile, the same special version will be executed
  444. for recursive invocations.
  445.    Also, any arguments that define variable values are added to `MAKE',
  446. so the sub-`make' gets them too.  Thus, if you do `make CFLAGS=-O', so
  447. that all C compilations will be optimized, the sub-`make' is run with
  448. `cd subdir; /bin/make CFLAGS=-O'.
  449.    The `MAKE' variable actually just refers to two other variables
  450. which contain these special values.  In fact, `MAKE' is always defined
  451. as `$(MAKE_COMMAND) $(MAKEOVERRIDES)'.  The variable `MAKE_COMMAND' is
  452. the file name with which `make' was invoked (such as `/bin/make',
  453. above).  The variable `MAKEOVERRIDES' contains definitions for the
  454. variables defined on the command line; in the above example, its value
  455. is `CFLAGS=-O'.  If you *do not* want these variable definitions done
  456. in all recursive `make' invocations, you can redefine the
  457. `MAKEOVERRIDES' variable to remove them.  You do this in any of the
  458. normal ways for defining variables: in a makefile (*note Setting
  459. Variables: Setting.); on the command line with an argument like
  460. `MAKEOVERRIDES=' (*note Overriding Variables: Overriding.); or with an
  461. environment variable (*note Variables from the Environment:
  462. Environment.).
  463.    As a special feature, using the variable `MAKE' in the commands of a
  464. rule alters the effects of the `-t' (`--touch'), `-n' (`--just-print'),
  465. or `-q' (`--question') option.  Using the `MAKE' variable has the same
  466. effect as using a `+' character at the beginning of the command line.
  467. *Note Instead of Executing the Commands: Instead of Execution.
  468.    Consider the command `make -t' in the above example.  (The `-t'
  469. option marks targets as up to date without actually running any
  470. commands; see *Note Instead of Execution::.)  Following the usual
  471. definition of `-t', a `make -t' command in the example would create a
  472. file named `subsystem' and do nothing else.  What you really want it to
  473. do is run `cd subdir; make -t'; but that would require executing the
  474. command, and `-t' says not to execute commands.
  475.    The special feature makes this do what you want: whenever a command
  476. line of a rule contains the variable `MAKE', the flags `-t', `-n' and
  477. `-q' do not apply to that line.  Command lines containing `MAKE' are
  478. executed normally despite the presence of a flag that causes most
  479. commands not to be run.  The usual `MAKEFLAGS' mechanism passes the
  480. flags to the sub-`make' (*note Communicating Options to a Sub-`make':
  481. Options/Recursion.), so your request to touch the files, or print the
  482. commands, is propagated to the subsystem.
  483. File: make.info,  Node: Variables/Recursion,  Next: Options/Recursion,  Prev: MAKE Variable,  Up: Recursion
  484. Communicating Variables to a Sub-`make'
  485. ---------------------------------------
  486.    Variable values of the top-level `make' can be passed to the
  487. sub-`make' through the environment by explicit request.  These
  488. variables are defined in the sub-`make' as defaults, but do not
  489. override what is specified in the sub-`make''s makefile unless you use
  490. the `-e' switch (*note Summary of Options: Options Summary.).
  491.    To pass down, or "export", a variable, `make' adds the variable and
  492. its value to the environment for running each command.  The sub-`make',
  493. in turn, uses the environment to initialize its table of variable
  494. values.  *Note Variables from the Environment: Environment.
  495.    Except by explicit request, `make' exports a variable only if it is
  496. either defined in the environment initially or set on the command line,
  497. and if its name consists only of letters, numbers, and underscores.
  498. Some shells cannot cope with environment variable names consisting of
  499. characters other than letters, numbers, and underscores.
  500.    The special variables `SHELL' and `MAKEFLAGS' are always exported.
  501. `MAKEFILES' is exported if you set it to anything.
  502.    Variables are *not* normally passed down if they were created by
  503. default by `make' (*note Variables Used by Implicit Rules: Implicit
  504. Variables.).  The sub-`make' will define these for itself.
  505.    If you want to export specific variables to a sub-`make', use the
  506. `export' directive, like this:
  507.      export VARIABLE ...
  508. If you want to *prevent* a variable from being exported, use the
  509. `unexport' directive, like this:
  510.      unexport VARIABLE ...
  511. As a convenience, you can define a variable and export it at the same
  512. time by doing:
  513.      export VARIABLE = value
  514. has the same result as:
  515.      VARIABLE = value
  516.      export VARIABLE
  517.      export VARIABLE := value
  518. has the same result as:
  519.      VARIABLE := value
  520.      export VARIABLE
  521.    Likewise,
  522.      export VARIABLE += value
  523. is just like:
  524.      VARIABLE += value
  525.      export VARIABLE
  526. *Note Appending More Text to Variables: Appending.
  527.    You may notice that the `export' and `unexport' directives work in
  528. `make' in the same way they work in the shell, `sh'.
  529.    If you want all variables to be exported by default, you can use
  530. `export' by itself:
  531.      export
  532. This tells `make' that variables which are not explicitly mentioned in
  533. an `export' or `unexport' directive should be exported.  Any variable
  534. given in an `unexport' directive will still *not* be exported.  If you
  535. use `export' by itself to export variables by default, variables whose
  536. names contain characters other than alphanumerics and underscores will
  537. not be exported unless specifically mentioned in an `export' directive.
  538.    The behavior elicited by an `export' directive by itself was the
  539. default in older versions of GNU `make'.  If your makefiles depend on
  540. this behavior and you want to be compatible with old versions of
  541. `make', you can write a rule for the special target
  542. `.EXPORT_ALL_VARIABLES' instead of using the `export' directive.  This
  543. will be ignored by old `make's, while the `export' directive will cause
  544. a syntax error.
  545.    Likewise, you can use `unexport' by itself to tell `make' *not* to
  546. export variables by default.  Since this is the default behavior, you
  547. would only need to do this if `export' had been used by itself earlier
  548. (in an included makefile, perhaps).  You *cannot* use `export' and
  549. `unexport' by themselves to have variables exported for some commands
  550. and not for others.  The last `export' or `unexport' directive that
  551. appears by itself determines the behavior for the entire run of `make'.
  552.    As a special feature, the variable `MAKELEVEL' is changed when it is
  553. passed down from level to level.  This variable's value is a string
  554. which is the depth of the level as a decimal number.  The value is `0'
  555. for the top-level `make'; `1' for a sub-`make', `2' for a
  556. sub-sub-`make', and so on.  The incrementation happens when `make' sets
  557. up the environment for a command.
  558.    The main use of `MAKELEVEL' is to test it in a conditional directive
  559. (*note Conditional Parts of Makefiles: Conditionals.); this way you can
  560. write a makefile that behaves one way if run recursively and another
  561. way if run directly by you.
  562.    You can use the variable `MAKEFILES' to cause all sub-`make'
  563. commands to use additional makefiles.  The value of `MAKEFILES' is a
  564. whitespace-separated list of file names.  This variable, if defined in
  565. the outer-level makefile, is passed down through the environment; then
  566. it serves as a list of extra makefiles for the sub-`make' to read
  567. before the usual or specified ones.  *Note The Variable `MAKEFILES':
  568. MAKEFILES Variable.
  569. File: make.info,  Node: Options/Recursion,  Next: -w Option,  Prev: Variables/Recursion,  Up: Recursion
  570. Communicating Options to a Sub-`make'
  571. -------------------------------------
  572.    Flags such as `-s' and `-k' are passed automatically to the
  573. sub-`make' through the variable `MAKEFLAGS'.  This variable is set up
  574. automatically by `make' to contain the flag letters that `make'
  575. received.  Thus, if you do `make -ks' then `MAKEFLAGS' gets the value
  576. `ks'.
  577.    As a consequence, every sub-`make' gets a value for `MAKEFLAGS' in
  578. its environment.  In response, it takes the flags from that value and
  579. processes them as if they had been given as arguments.  *Note Summary
  580. of Options: Options Summary.
  581.    The options `-C', `-f', `-I', `-o', and `-W' are not put into
  582. `MAKEFLAGS'; these options are not passed down.
  583.    The `-j' option is a special case (*note Parallel Execution:
  584. Parallel.).  If you set it to some numeric value, `-j 1' is always put
  585. into `MAKEFLAGS' instead of the value you specified.  This is because if
  586. the `-j' option were passed down to sub-`make's, you would get many
  587. more jobs running in parallel than you asked for.  If you give `-j'
  588. with no numeric argument, meaning to run as many jobs as possible in
  589. parallel, this is passed down, since multiple infinities are no more
  590. than one.
  591.    If you do not want to pass the other flags down, you must change the
  592. value of `MAKEFLAGS', like this:
  593.      MAKEFLAGS=
  594.      subsystem:
  595.              cd subdir; $(MAKE)
  596.    or like this:
  597.      subsystem:
  598.              cd subdir; $(MAKE) MAKEFLAGS=
  599.    A similar variable `MFLAGS' exists also, for historical
  600. compatibility.  It has the same value as `MAKEFLAGS' except that a
  601. hyphen is added at the beginning if it is not empty.  `MFLAGS' was
  602. traditionally used explicitly in the recursive `make' command, like
  603. this:
  604.      subsystem:
  605.              cd subdir; $(MAKE) $(MFLAGS)
  606. but now `MAKEFLAGS' makes this usage redundant.
  607.    The `MAKEFLAGS' and `MFLAGS' variables can also be useful if you
  608. want to have certain options, such as `-k' (*note Summary of Options:
  609. Options Summary.) set each time you run `make'.  Just put `MAKEFLAGS=k'
  610. or `MFLAGS=-k' in your environment.  These variables may also be set in
  611. makefiles, so a makefile can specify additional flags that should also
  612. be in effect for that makefile.
  613.    If you do put `MAKEFLAGS' or `MFLAGS' in your environment, you
  614. should be sure not to include any options that will drastically affect
  615. the actions of `make' and undermine the purpose of makefiles and of
  616. `make' itself.  For instance, the `-t', `-n', and `-q' options, if put
  617. in one of these variables, could have disastrous consequences and would
  618. certainly have at least surprising and probably annoying effects.
  619. File: make.info,  Node: -w Option,  Prev: Options/Recursion,  Up: Recursion
  620. The `--print-directory' Option
  621. ------------------------------
  622.    If you use several levels of recursive `make' invocations, the `-w'
  623. or `--print-directory' option can make the output a lot easier to
  624. understand by showing each directory as `make' starts processing it and
  625. as `make' finishes processing it.  For example, if `make -w' is run in
  626. the directory `/u/gnu/make', `make' will print a line of the form:
  627.      make: Entering directory `/u/gnu/make'.
  628. before doing anything else, and a line of the form:
  629.      make: Leaving directory `/u/gnu/make'.
  630. when processing is completed.
  631.    Normally, you do not need to specify this option because `make' does
  632. it for you: `-w' is turned on automatically when you use the `-C'
  633. option, and in sub-`make's.  `make' will not automatically turn on `-w'
  634. if you also use `-s', which says to be silent, or if you use
  635. `--no-print-directory' to explicitly disable it.
  636. File: make.info,  Node: Sequences,  Next: Empty Commands,  Prev: Recursion,  Up: Commands
  637. Defining Canned Command Sequences
  638. =================================
  639.    When the same sequence of commands is useful in making various
  640. targets, you can define it as a canned sequence with the `define'
  641. directive, and refer to the canned sequence from the rules for those
  642. targets.  The canned sequence is actually a variable, so the name must
  643. not conflict with other variable names.
  644.    Here is an example of defining a canned sequence of commands:
  645.      define run-yacc
  646.      yacc $(firstword $^)
  647.      mv y.tab.c $@
  648.      endef
  649. Here `run-yacc' is the name of the variable being defined; `endef'
  650. marks the end of the definition; the lines in between are the commands.
  651. The `define' directive does not expand variable references and
  652. function calls in the canned sequence; the `$' characters, parentheses,
  653. variable names, and so on, all become part of the value of the variable
  654. you are defining.  *Note Defining Variables Verbatim: Defining, for a
  655. complete explanation of `define'.
  656.    The first command in this example runs Yacc on the first dependency
  657. of whichever rule uses the canned sequence.  The output file from Yacc
  658. is always named `y.tab.c'.  The second command moves the output to the
  659. rule's target file name.
  660.    To use the canned sequence, substitute the variable into the
  661. commands of a rule.  You can substitute it like any other variable
  662. (*note Basics of Variable References: Reference.).  Because variables
  663. defined by `define' are recursively expanded variables, all the
  664. variable references you wrote inside the `define' are expanded now.
  665. For example:
  666.      foo.c : foo.y
  667.              $(run-yacc)
  668. `foo.y' will be substituted for the variable `$^' when it occurs in
  669. `run-yacc''s value, and `foo.c' for `$@'.
  670.    This is a realistic example, but this particular one is not needed in
  671. practice because `make' has an implicit rule to figure out these
  672. commands based on the file names involved (*note Using Implicit Rules:
  673. Implicit Rules.).
  674.    In command execution, each line of a canned sequence is treated just
  675. as if the line appeared on its own in the rule, preceded by a tab.  In
  676. particular, `make' invokes a separate subshell for each line.  You can
  677. use the special prefix characters that affect command lines (`@', `-',
  678. and `+') on each line of a canned sequence.  *Note Writing the Commands
  679. in Rules: Commands.  For example, using this canned sequence:
  680.      define frobnicate
  681.      @echo "frobnicating target $@"
  682.      frob-step-1 $< -o $@-step-1
  683.      frob-step-2 $@-step-1 -o $@
  684.      endef
  685. `make' will not echo the first line, the `echo' command.  But it *will*
  686. echo the following two command lines.
  687.    On the other hand, prefix characters on the command line that refers
  688. to a canned sequence apply to every line in the sequence.  So the rule:
  689.      frob.out: frob.in
  690.          @$(frobnicate)
  691. does not echo *any* commands.  (*Note Command Echoing: Echoing, for a
  692. full explanation of `@'.)
  693. File: make.info,  Node: Empty Commands,  Prev: Sequences,  Up: Commands
  694. Using Empty Commands
  695. ====================
  696.    It is sometimes useful to define commands which do nothing.  This is
  697. done simply by giving a command that consists of nothing but
  698. whitespace.  For example:
  699.      target: ;
  700. defines an empty command string for `target'.  You could also use a
  701. line beginning with a tab character to define an empty command string,
  702. but this would be confusing because such a line looks empty.
  703.    You may be wondering why you would want to define a command string
  704. that does nothing.  The only reason this is useful is to prevent a
  705. target from getting implicit commands (from implicit rules or the
  706. `.DEFAULT' special target; *note Implicit Rules::. and *note Defining
  707. Last-Resort Default Rules: Last Resort.).
  708.    You may be inclined to define empty command strings for targets that
  709. are not actual files, but only exist so that their dependencies can be
  710. remade.  However, this is not the best way to do that, because the
  711. dependencies may not be remade properly if the target file actually
  712. does exist.  *Note Phony Targets: Phony Targets, for a better way to do
  713. this.
  714. File: make.info,  Node: Using Variables,  Next: Conditionals,  Prev: Commands,  Up: Top
  715. How to Use Variables
  716. ********************
  717.    A "variable" is a name defined in a makefile to represent a string
  718. of text, called the variable's "value".  These values are substituted
  719. by explicit request into targets, dependencies, commands, and other
  720. parts of the makefile.  (In some other versions of `make', variables
  721. are called "macros".)
  722.    Variables and functions in all parts of a makefile are expanded when
  723. read, except for the shell commands in rules, the right-hand sides of
  724. variable definitions using `=', and the bodies of variable definitions
  725. using the `define' directive.
  726.    Variables can represent lists of file names, options to pass to
  727. compilers, programs to run, directories to look in for source files,
  728. directories to write output in, or anything else you can imagine.
  729.    A variable name may be any sequence of characters not containing `:',
  730. `#', `=', or leading or trailing whitespace.  However, variable names
  731. containing characters other than letters, numbers, and underscores
  732. should be avoided, as they may be given special meanings in the future,
  733. and with some shells they cannot be passed through the environment to a
  734. sub-`make' (*note Communicating Variables to a Sub-`make':
  735. Variables/Recursion.).
  736.    It is traditional to use upper case letters in variable names, but we
  737. recommend using lower case letters for variable names that serve
  738. internal purposes in the makefile, and reserving upper case for
  739. parameters that control implicit rules or for parameters that the user
  740. should override with command options (*note Overriding Variables:
  741. Overriding.).
  742. * Menu:
  743. * Reference::                   How to use the value of a variable.
  744. * Flavors::                     Variables come in two flavors.
  745. * Advanced::                    Advanced features for referencing a variable.
  746. * Values::                      All the ways variables get their values.
  747. * Setting::                     How to set a variable in the makefile.
  748. * Appending::                   How to append more text to the old value
  749.                                   of a variable.
  750. * Override Directive::          How to set a variable in the makefile even if
  751.                                   the user has set it with a command argument.
  752. * Defining::                    An alternate way to set a variable
  753.                                   to a verbatim string.
  754. * Environment::                 Variable values can come from the environment.
  755. File: make.info,  Node: Reference,  Next: Flavors,  Up: Using Variables
  756. Basics of Variable References
  757. =============================
  758.    To substitute a variable's value, write a dollar sign followed by
  759. the name of the variable in parentheses or braces: either `$(foo)' or
  760. `${foo}' is a valid reference to the variable `foo'.  This special
  761. significance of `$' is why you must write `$$' to have the effect of a
  762. single dollar sign in a file name or command.
  763.    Variable references can be used in any context: targets,
  764. dependencies, commands, most directives, and new variable values.  Here
  765. is an example of a common case, where a variable holds the names of all
  766. the object files in a program:
  767.      objects = program.o foo.o utils.o
  768.      program : $(objects)
  769.              cc -o program $(objects)
  770.      
  771.      $(objects) : defs.h
  772.    Variable references work by strict textual substitution.  Thus, the
  773.      foo = c
  774.      prog.o : prog.$(foo)
  775.              $(foo)$(foo) -$(foo) prog.$(foo)
  776. could be used to compile a C program `prog.c'.  Since spaces before the
  777. variable value are ignored in variable assignments, the value of `foo'
  778. is precisely `c'.  (Don't actually write your makefiles this way!)
  779.    A dollar sign followed by a character other than a dollar sign,
  780. open-parenthesis or open-brace treats that single character as the
  781. variable name.  Thus, you could reference the variable `x' with `$x'.
  782. However, this practice is strongly discouraged, except in the case of
  783. the automatic variables (*note Automatic Variables: Automatic.).
  784. File: make.info,  Node: Flavors,  Next: Advanced,  Prev: Reference,  Up: Using Variables
  785. The Two Flavors of Variables
  786. ============================
  787.    There are two ways that a variable in GNU `make' can have a value;
  788. we call them the two "flavors" of variables.  The two flavors are
  789. distinguished in how they are defined and in what they do when expanded.
  790.    The first flavor of variable is a "recursively expanded" variable.
  791. Variables of this sort are defined by lines using `=' (*note Setting
  792. Variables: Setting.) or by the `define' directive (*note Defining
  793. Variables Verbatim: Defining.).  The value you specify is installed
  794. verbatim; if it contains references to other variables, these
  795. references are expanded whenever this variable is substituted (in the
  796. course of expanding some other string).  When this happens, it is
  797. called "recursive expansion".
  798.    For example,
  799.      foo = $(bar)
  800.      bar = $(ugh)
  801.      ugh = Huh?
  802.      
  803.      all:;echo $(foo)
  804. will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to
  805. `$(ugh)' which finally expands to `Huh?'.
  806.    This flavor of variable is the only sort supported by other versions
  807. of `make'.  It has its advantages and its disadvantages.  An advantage
  808. (most would say) is that:
  809.      CFLAGS = $(include_dirs) -O
  810.      include_dirs = -Ifoo -Ibar
  811. will do what was intended: when `CFLAGS' is expanded in a command, it
  812. will expand to `-Ifoo -Ibar -O'.  A major disadvantage is that you
  813. cannot append something on the end of a variable, as in
  814.      CFLAGS = $(CFLAGS) -O
  815. because it will cause an infinite loop in the variable expansion.
  816. (Actually `make' detects the infinite loop and reports an error.)
  817.    Another disadvantage is that any functions (*note Functions for
  818. Transforming Text: Functions.) referenced in the definition will be
  819. executed every time the variable is expanded.  This makes `make' run
  820. slower; worse, it causes the `wildcard' and `shell' functions to give
  821. unpredictable results because you cannot easily control when they are
  822. called, or even how many times.
  823.    To avoid all the problems and inconveniences of recursively expanded
  824. variables, there is another flavor: simply expanded variables.
  825.    "Simply expanded variables" are defined by lines using `:=' (*note
  826. Setting Variables: Setting.).  The value of a simply expanded variable
  827. is scanned once and for all, expanding any references to other
  828. variables and functions, when the variable is defined.  The actual
  829. value of the simply expanded variable is the result of expanding the
  830. text that you write.  It does not contain any references to other
  831. variables; it contains their values *as of the time this variable was
  832. defined*.  Therefore,
  833.      x := foo
  834.      y := $(x) bar
  835.      x := later
  836. is equivalent to
  837.      y := foo bar
  838.      x := later
  839.    When a simply expanded variable is referenced, its value is
  840. substituted verbatim.
  841.    Here is a somewhat more complicated example, illustrating the use of
  842. `:=' in conjunction with the `shell' function.  (*Note The `shell'
  843. Function: Shell Function.)  This example also shows use of the variable
  844. `MAKELEVEL', which is changed when it is passed down from level to
  845. level.  (*Note Communicating Variables to a Sub-`make':
  846. Variables/Recursion, for information about `MAKELEVEL'.)
  847.      ifeq (0,${MAKELEVEL})
  848.      cur-dir   := $(shell pwd)
  849.      whoami    := $(shell whoami)
  850.      host-type := $(shell arch)
  851.      MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
  852.      endif
  853. An advantage of this use of `:=' is that a typical `descend into a
  854. directory' command then looks like this:
  855.      ${subdirs}:
  856.            ${MAKE} cur-dir=${cur-dir}/$@ -C $@ all
  857.    Simply expanded variables generally make complicated makefile
  858. programming more predictable because they work like variables in most
  859. programming languages.  They allow you to redefine a variable using its
  860. own value (or its value processed in some way by one of the expansion
  861. functions) and to use the expansion functions much more efficiently
  862. (*note Functions for Transforming Text: Functions.).
  863.    You can also use them to introduce controlled leading or trailing
  864. spaces into variable values.  Such spaces are discarded from your input
  865. before substitution of variable references and function calls; this
  866. means you can include leading or trailing spaces in a variable value by
  867. protecting them with variable references, like this:
  868.      nullstring :=
  869.      space := $(nullstring) $(nullstring)
  870. Here the value of the variable `space' is precisely one space.
  871. File: make.info,  Node: Advanced,  Next: Values,  Prev: Flavors,  Up: Using Variables
  872. Advanced Features for Reference to Variables
  873. ============================================
  874.    This section describes some advanced features you can use to
  875. reference variables in more flexible ways.
  876. * Menu:
  877. * Substitution Refs::           Referencing a variable with
  878.                                   substitutions on the value.
  879. * Computed Names::              Computing the name of the variable to refer to.
  880. File: make.info,  Node: Substitution Refs,  Next: Computed Names,  Up: Advanced
  881. Substitution References
  882. -----------------------
  883.    A "substitution reference" substitutes the value of a variable with
  884. alterations that you specify.  It has the form `$(VAR:A=B)' (or
  885. `${VAR:A=B}') and its meaning is to take the value of the variable VAR,
  886. replace every A at the end of a word with B in that value, and
  887. substitute the resulting string.
  888.    When we say "at the end of a word", we mean that A must appear
  889. either followed by whitespace or at the end of the value in order to be
  890. replaced; other occurrences of A in the value are unaltered.  For
  891. example:
  892.      foo := a.o b.o c.o
  893.      bar := $(foo:.o=.c)
  894. sets `bar' to `a.c b.c c.c'.  *Note Setting Variables: Setting.
  895.    A substitution reference is actually an abbreviation for use of the
  896. `patsubst' expansion function (*note Functions for String Substitution
  897. and Analysis: Text Functions.).  We provide substitution references as
  898. well as `patsubst' for compatibility with other implementations of
  899. `make'.
  900.    Another type of substitution reference lets you use the full power of
  901. the `patsubst' function.  It has the same form `$(VAR:A=B)' described
  902. above, except that now A must contain a single `%' character.  This
  903. case is equivalent to `$(patsubst A,B,$(VAR))'.  *Note Functions for
  904. String Substitution and Analysis: Text Functions, for a description of
  905. the `patsubst' function.
  906. For example:
  907.      foo := a.o b.o c.o
  908.      bar := $(foo:%.o=%.c)
  909. sets `bar' to `a.c b.c c.c'.
  910.